﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using OxyPlot;
using OxyPlot.Axes;
using OxyPlot.Series;
using DetectingKinectSencor._40_Tools;

namespace DetectingKinectSencor._50_Evaluation.ViewModels.Dashboard
{
    public class ViewingDirectionViewModelDashboard : Notifier
    {
        // Internal variable in which the model is saved in this class
        private PlotModel plotModel;
        public PlotModel tempPlotModel;
        private LineSeries timepointSerie;
        private TimeSpan totalAmountLookedAway;

        // Important the UI needs to bind the Model to ViewingDirectionPlotModel!!!
        public PlotModel ViewingDirectionPlotModel
        {
            get { return plotModel; }
            set { plotModel = value; RaisePropertyChanged(() => ViewingDirectionPlotModel); }
        }


        public ViewingDirectionViewModelDashboard(DateTime lastElementCaptureTime)
        {
            tempPlotModel = new PlotModel();
            SetUpModel();
            LoadData(lastElementCaptureTime);
         }

        private void SetUpModel()
        {
            tempPlotModel.LegendTitle = "Legend";
            tempPlotModel.LegendOrientation = LegendOrientation.Horizontal;
            tempPlotModel.LegendPlacement = LegendPlacement.Outside;
            tempPlotModel.LegendPosition = LegendPosition.TopRight;
            tempPlotModel.LegendBackground = OxyColor.FromAColor(200, OxyColors.White);
            tempPlotModel.LegendBorder = OxyColors.Black;
            tempPlotModel.LegendFontSize = DesignOfGraphs.fontSizeDashboard;


            // Axes which shows the date when the speaker looked away
            TimeSpanAxis timeLineAxis = new TimeSpanAxis(AxisPosition.Bottom, "Timeline (mm:ss)", "mm:ss")
                                            {
                                                MajorGridlineStyle = LineStyle.Solid,
                                                MinorGridlineStyle = LineStyle.Dot,
                                                FontSize = DesignOfGraphs.fontSizeDashboard
                                            };

            // Add the axes to the plot model
            tempPlotModel.Axes.Add(timeLineAxis);

            // Axes which shows the Amount of time the speaker looked away
            TimeSpanAxis timeSpanAxis = new TimeSpanAxis(AxisPosition.Left, "Looked away (mm:ss)", "mm:ss")
                                                {
                                                    MajorGridlineStyle = LineStyle.Solid,
                                                    MinorGridlineStyle = LineStyle.Dot,
                                                    FontSize = DesignOfGraphs.fontSizeDashboard
                                                };
            // Add the axes to the plot model
            tempPlotModel.Axes.Add(timeSpanAxis);

        }

        public void LoadData(DateTime captureTime)
        {
            // Get the list with the viewingDirectionData
            ViewingDirectionData viewingDirectionList = EvaluationData.viewingDirectionData;
            TimeSpan elapsedTime;

            timepointSerie = new LineSeries
            {
                StrokeThickness = 2,
                MarkerSize = 3,
                MarkerStroke = Graph.colors[1],
                MarkerType = Graph.markerTypes[1],
                CanTrackerInterpolatePoints = false,
                Title = string.Format("Viewing direction"),
                Smooth = false,
            };

            totalAmountLookedAway = new TimeSpan(0, 0, 0);

            foreach (CaptureTime viewingDirection in viewingDirectionList.trackingSequences)
            {
                if (captureTime == viewingDirection.captureTime)
                {

                    for (int i = 0; i < viewingDirection.timePoints.Count; i++)
                    {
                        totalAmountLookedAway += viewingDirection.timePoints[i].timeSpan;

                        // It is more intuitiv to see the actual elapsed time from the beginning then the actual time 
                        elapsedTime = viewingDirection.timePoints[i].timePoint - captureTime;
                        timepointSerie.Points.Add(new DataPoint(TimeSpanAxis.ToDouble(elapsedTime),
                                                                TimeSpanAxis.ToDouble(viewingDirection.timePoints[i].timeSpan)));
                    }

                    tempPlotModel.Series.Add(timepointSerie);

                    break;
                }
            }

            Console.WriteLine("totalAmountLookedAway: " + totalAmountLookedAway.TotalMinutes);

            //Saved the tempPlotModel to the plotModel than is the OnPropertyChanged executed and the UI is updated
            ViewingDirectionPlotModel = tempPlotModel;
        }
    }
}
